package cn.cool.service.impl;

import cn.cool.entity.UserLikeArticle;
import cn.cool.mapper.UserLikeArticleMapper;
import cn.cool.service.UserLikeArticleService;
import cn.cool.utils.JWTUtils;
import cn.cool.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LiuH
 * @since 2021-01-23
 */
@Service
public class UserLikeArticleServiceImpl extends ServiceImpl<UserLikeArticleMapper, UserLikeArticle> implements UserLikeArticleService {

    @Autowired
    private UserLikeArticleMapper userLikeArticleMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 设置用户 点赞/取消点赞
     *
     * @param request 根据token获取用户id
     * @param artId   文章id
     * @return
     */
    @Override
    public void setUserLike(HttpServletRequest request, Integer artId) {

        //获取当前用户id
        Integer userId = JWTUtils.getUserId(request);
        String item = userId + ":" + artId;
        //从缓存中读取点赞数据
        Integer isLike = (Integer) redisUtil.hget("userLikeArticle", item);
        //如果为空 说明是第一次点赞
        if (isLike == null) {

            redisUtil.hset("userLikeArticle", item, 1);
            //点赞总量 +1
            redisUtil.hincr("artLikeCount", "" + artId, 1);

        } else if (isLike.equals(0)) { //值为 0 设置值为 1,反之设置为 0(取消点赞状态)
            redisUtil.hset("userLikeArticle", item, 1);
            //点赞总量 +1
            redisUtil.hincr("artLikeCount", "" + artId, 1);
        } else {
            redisUtil.hset("userLikeArticle", item, 0);
            //点赞总量 -1
            redisUtil.hincr("artLikeCount", "" + artId, -1);
        }

    }

    /**
     * 将缓存中的点赞数据 持久化到数据库
     */
    @Override
    public int saveArtLikeRedisToDB() {

        //获取缓存中所有点赞数据
        Map<Object, Object> userLikeInfo = redisUtil.hmget("userLikeArticle");

        //存放点赞信息的容器
        List<UserLikeArticle> saveUserLikeArticles = new ArrayList<>();

        for (Map.Entry entry : userLikeInfo.entrySet()) {
            String key = (String) entry.getKey();
            //分割key
            String[] split = key.split(":");
            //点赞的用户
            Integer userId = Integer.valueOf(split[0]);
            //被点赞的文章
            Integer artId = Integer.valueOf(split[1]);
            //点赞状态 0没点 1点了
            Integer value = (Integer) entry.getValue();

            UserLikeArticle userLikeArticle;
            if (value.equals(1)) {
                userLikeArticle = new UserLikeArticle(userId, artId, true);
            } else {
                userLikeArticle = new UserLikeArticle(userId, artId, false);
            }
            saveUserLikeArticles.add(userLikeArticle);
        }

        //批量插入数据库
        return userLikeArticleMapper.insertBatchByRedis(saveUserLikeArticles);

    }

    /**
     * 清除缓存的点赞数据
     */
    @Override
    public void clearCache() {
        redisUtil.del("userLikeArticle");
    }



}
